<template>
  <el-dialog
    append-to-body
    title="完成工单"
    width="30%"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="handleClose">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="最终服务费(元)" prop="serviceFee">
        <el-input v-model="form.serviceFee"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="form.remarks" :rows="4"></el-input>
      </el-form-item>
      <el-form-item label="是否已付款">
        <el-radio-group v-model="form.paid">
          <el-radio :label="false">否</el-radio>
          <el-radio :label="true">是</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close"  icon="el-icon-circle-close">取 消</el-button>
      <el-button icon="el-icon-circle-check" type="primary" :loading="submitBtnLoading" @click="submit()">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {doneworkorder} from "@/api/market/repair/order/person"

export default {
  name: 'FinishWorkOrder',
  components: { },
  props: {
    visible: {
      type: Boolean
    },
    id: [Number, String],
    serviceFee: [Number, String]
  },
  data() {
    return {
      dialogVisible: false,
      submitBtnLoading: false,
      form: {},
      rules: {
        serviceFee: [
          {required: true, trigger: 'blur', message: '服务费不能为空'},
          {
            pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
            message: '请输入正确金额格式，可保留两位小数'
          }
        ]
      }
    }
  },
  methods: {
    handleClose(done) {
      this.$refs.form.resetFields()
      this.form = {}
      this.$emit('on-close')
      done()
    },
    close() {
      this.$refs.form.resetFields()
      this.form = {}
      this.$emit('on-close')
      this.dialogVisible = false
    },
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitBtnLoading = true
          this.form.id = this.id
          doneworkorder(this.form).then((res) => {
            this.$refs.form.resetFields()
            this.form = {}
            this.dialogVisible = false
            this.$emit('on-success')
          })

        }
      })
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.$set(this.form, 'serviceFee', this.serviceFee)
        this.$set(this.form, 'paid', false)
        this.dialogVisible = val
      }
    }
  }
}
</script>
